package cn.edu.sdju.fengguoyu.util;

import com.aliyun.alidns20150109.models.UpdateDomainRecordResponse;
import com.aliyun.tea.TeaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class AhoUtil {
    static Logger logger = LoggerFactory.getLogger(AhoUtil.class);
    private static com.aliyun.alidns20150109.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "alidns.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.alidns20150109.Client(config);
    }
    public static void updateAliyunDNSRecord(String accessKeyId, String accessKeySecret, String address) {
        com.aliyun.alidns20150109.Client client = null;
        try {
            client = AhoUtil.createClient(accessKeyId, accessKeySecret);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        com.aliyun.alidns20150109.models.UpdateDomainRecordRequest updateDomainRecordRequest = new com.aliyun.alidns20150109.models.UpdateDomainRecordRequest()
                .setRecordId("819187770992071680")
                .setRR("reg.sdju")
                .setType("A")
                .setValue(address);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            UpdateDomainRecordResponse response = client.updateDomainRecordWithOptions(updateDomainRecordRequest, runtime);
            logger.info("成功设置DNS解析");
        } catch (TeaException error) {
            logger.error(error.getMessage());
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            logger.error(error.getMessage());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
    public static String getLocalIPv4Address() {
        try {
            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
            while (nifs.hasMoreElements()) {
                NetworkInterface networkInterface = nifs.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                if (networkInterface.getName().startsWith("wlan")) {
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        if (address instanceof Inet4Address) {
                            return address.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    public static double ipDistance(String ip1, String ip2) {
        if (!ip1.matches("(\\d{1,3}\\.){3}\\d{1,3}") || !ip2.matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
            return -1;
        }
        int num1 = 0;
        int num2 = 0;
        String[] parts1 = ip1.split("\\.");
        String[] parts2 = ip2.split("\\.");
        for (int i = 0; i < 4; i++) {
            int part1 = Integer.parseInt(parts1[i]);
            int part2 = Integer.parseInt(parts2[i]);
            if (part1 < 0 || part1 > 255 || part2 < 0 || part2 > 255) {
                return -1;
            }
            num1 = (num1 << 8) | part1;
            num2 = (num2 << 8) | part2;
        }

        int diff = num1 ^ num2;
        double sum = 0;
        double weight = 32;
        int mask = 0x80000000;
        while (mask != 0) {
            if ((diff & mask) != 0) {

                sum += weight;
            }
            mask = mask >>> 1;
            weight /= 2;
        }

        return sum;
    }
}
